(function(){"use strict";BX.namespace("BX.Landing.UI.Field");var i=BX.Landing.Utils.addClass;var t=BX.Landing.Utils.hasClass;var n=BX.Landing.Utils.removeClass;var e=BX.Landing.Utils.isArray;var a=BX.Landing.Utils.isFunction;var s=BX.Landing.Utils.create;var l=BX.Landing.Utils.random;var o=BX.Landing.Utils.escapeHtml;var u=BX.Landing.Utils.append;var d=BX.Landing.Utils.slice;var p=BX.Landing.Utils.encodeDataValue;var r=BX.Landing.Utils.remove;var h=BX.Landing.Utils.data;var c=BX.Landing.Utils.bind;var g=BX.Landing.Utils.style;var f=BX.Landing.Utils.clone;var v=BX.Landing.Utils.offsetLeft;var m=BX.Landing.Utils.offsetTop;var B=BX.Landing.Utils.findParent;var C=BX.Landing.Utils.decodeDataValue;var L=BX.Landing.Collection.BaseCollection;function X(i,t,n){i.forEach(function(i){var a=new BX.Landing.UI.Field.Checkbox({id:i.value,items:[{name:i.name,value:i.value,checked:i.selected}],depth:n,compact:true,onChange:t.onCheckboxChange});t.fields.add(a);if(a.layout){u(a.layout,t.getPopup().contentContainer)}if(i.selected){t.addPlaceholder(i)}if(e(i.items)){n+=1;X(i.items,t,n);n-=1}})}BX.Landing.UI.Field.MultiSelect=function(t){BX.Landing.UI.Field.BaseField.apply(this,arguments);i(this.layout,"landing-ui-field-multiselect");this.onChangeHandler=a(t.onChange)?t.onChange:function(){};this.items=e(t.items)?t.items:[];this.value=e(t.value)?t.value:null;this.content=this.value;this.fields=new L;this.button=new BX.Landing.UI.Button.BaseButton("dropdown_button",{text:BX.Landing.Loc.getMessage("LINK_URL_SUGGESTS_SELECT"),className:"landing-ui-button-select-link",onClick:this.onButtonClick.bind(this)});this.grid=s("div",{props:{className:"landing-ui-field-multiselect-grid"},children:[s("div",{props:{className:"landing-ui-field-multiselect-grid-left"},children:[this.input]}),s("div",{props:{className:"landing-ui-field-multiselect-grid-right"},children:[this.button.layout]})]});u(this.grid,this.layout);this.onInputClick=this.onInputClick.bind(this);this.onCheckboxChange=this.onCheckboxChange.bind(this);var n=BX.Landing.PageObject.getRootWindow();c(this.input,"click",this.onInputClick);c(n.document,"click",this.onDocumentClick.bind(this));requestAnimationFrame(function(){X(this.items,this,0);if(e(this.value)){this.value=this.value.map(function(i){return C(i)});this.setValue(this.value,true)}else{this.value=this.getValue();this.content=this.value}}.bind(this))};BX.Landing.UI.Field.MultiSelect.prototype={constructor:BX.Landing.UI.Field.MultiSelect,__proto__:BX.Landing.UI.Field.BaseField.prototype,addPlaceholder:function(i){var t=s("div",{props:{className:"landing-ui-field-multiselect-placeholder"},attrs:{"data-item":p(i),title:o(i.name)},children:[s("span",{props:{className:"landing-ui-field-multiselect-placeholder-text"},html:o(i.name)}),s("span",{props:{className:"landing-ui-field-multiselect-placeholder-remove"},events:{click:this.onPlaceholderRemoveClick.bind(this,i)}})]});u(t,this.input)},onPlaceholderRemoveClick:function(i,t,n){if(t){t.preventDefault();t.stopPropagation()}var e=this.getPlaceholderByItem(i);if(e){r(e);this.adjustPopupPosition();var a=this.fields.get(i.value);if(a){a.layout.querySelector("input").checked=false}}if(!n){this.onValueChangeHandler(this)}},onCheckboxChange:function(i){if(i instanceof BX.Landing.UI.Field.Checkbox){var t=i.getValue();if(t.length){this.addPlaceholder(i.items[0]);this.adjustPopupPosition()}else{this.onPlaceholderRemoveClick(i.items[0],null,true)}this.onValueChangeHandler(this)}},getPlaceholderByItem:function(i){return d(this.input.children).find(function(t){return h(t,"data-item").value===i.value})},getPopup:function(){if(this.popup){return this.popup}this.popup=new BX.Main.Popup({id:this.selector+"_"+l(),bindElement:this.input,autoHide:true,maxHeight:142,events:{onPopupClose:function(){n(this.input,"landing-ui-active");if(t(this.layout.parentElement.parentElement,"landing-ui-form-style")){void g(this.layout.parentElement.parentElement,{"z-index":null,position:null})}}.bind(this)}});if(this.popup.popupContainer){i(this.popup.popupContainer,"landing-ui-field-multiselect-popup");var e=B(this.input,{className:"landing-ui-panel-content-body-content"},document.body);if(e){u(this.popup.popupContainer,e)}}return this.popup},showPopup:function(){this.getPopup().show();this.adjustPopupPosition();this.setValue(this.getValue(),true)},adjustPopupPosition:function(){if(this.popup){var i=B(this.input,{className:"landing-ui-panel-content-body-content"});if(BX.Type.isDomNode(i)){var t=m(this.input,i);var n=v(this.input,i);var e=this.input.getBoundingClientRect();var a=2;requestAnimationFrame(function(){this.popup.popupContainer.style.top=t+e.height+a+"px";this.popup.popupContainer.style.left=n+"px";this.popup.popupContainer.style.width=e.width+"px"}.bind(this))}}},onInputClick:function(t){if(t){t.stopPropagation()}var e=this.getPopup();if(e.isShown()){n(this.input,"landing-ui-active");return e.close()}i(this.input,"landing-ui-active");return this.showPopup()},onButtonClick:function(i){i.preventDefault();i.stopPropagation();this.onInputClick()},onDocumentClick:function(){this.getPopup().close()},getValue:function(){return d(this.input.children).map(function(i){return h(i,"data-item").value})},isChanged:function(){var i=f(this.content).sort();var t=this.getValue().sort();return JSON.stringify(i)!==JSON.stringify(t)},getItemByValue:function(i,t){var n=null;function e(i,t){return i.forEach(function(i){if(t==i.value){n=i;return}if(i.items){e(i.items,t)}},this)}e(i,t);return n},setValue:function(i,t){if(e(i)){this.input.innerHTML="";i.forEach(function(i){var t=this.getItemByValue(this.items,i);if(t){this.addPlaceholder(t);var n=this.fields.find(function(t){return t.id===i}.bind(this));if(n){n.setValue([i])}}},this)}if(!t){this.onValueChangeHandler(this)}}}})();
//# sourceMappingURL=multiselect_field.map.js